﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Commonlib;

namespace AlbonWeb.Manager.OrderManager
{
    public partial class PayRecord : System.Web.UI.Page
    {
        BLL.InvoiceBLL invoicebll = new BLL.InvoiceBLL();
        BLL.PayMentBLL paymentbll = new BLL.PayMentBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //加载数据
                if (Request["invoiceNo"] != null)
                {
                    bindPayMent();
                }
            }
        }

        //绑定付款信息
        private void bindPayMent()
        {
            string invoiceNo = Request["invoiceNo"].ToString();
            initData(invoiceNo);
            string cmdstr = "select * from PayMent where invoiceNo=" + invoiceNo;
            SqlHelper.BindRepeater(repMain,SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null);
        }

        private void initData(string invoiceNo)
        {
            DataTable dt= invoicebll.getInvoiceByNo(invoiceNo);
            if (dt.Rows.Count > 0)
            {
                refNo.Text = dt.Rows[0]["refNo"].ToString();
                finalTotal.Text = decimal.Parse(dt.Rows[0]["totalMoney"].ToString()).ToString("0.00");
                decimal balance=paymentbll.Balance(invoiceNo);
                Balance.Text =balance.ToString("0.00");
            }
        }

        //添加收款记录
        protected void addPay_Click(object sender, EventArgs e)
        {
            Albon.Model.PayMent payment = new Albon.Model.PayMent();
            payment.invoiceNo =int.Parse(Request["invoiceNo"].ToString());
            payment.payMoney = decimal.Parse(payMoney.Text.Trim());
            payment.receiver = User.Identity.Name;
            payment.remark = remark.Text.Trim();
            paymentbll.addPayMent(payment);
            bindPayMent();
        }

        //删除信息
        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            string paymentID = ((ImageButton)sender).CommandName.ToString();
            if (paymentbll.deletePayMent(paymentID))
            {
                bindPayMent();
            }
        }
    }
}